package com.ndp.fb.walle.util;

import com.ndp.fb.constant.CommonConstants;
import com.ndp.fb.enums.targetingspec.PageType;
import com.ndp.fb.model.business.bo.targetingspec.CustomAudience;
import com.ndp.fb.model.business.vo.cache.result.FbUserVo;
import com.ndp.fb.rdb.model.FbUser;
import com.ndp.fb.util.ListUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hunk.qin on 2016/2/16.
 */
public class BuildCacheUtils {

    public static void ageAppend(Integer ageMin, Integer ageMax, StringBuilder ageSb) {
        if (0 != Integer.compare(ageMin, ageMax)) {
            ageSb.append(CommonConstants.SYMBOL_LINE);
            ageSb.append(ageMax);
            //如果年龄上限是no max,则显示65+
            if (0 <= Integer.compare(ageMax, CommonConstants.NUM_INT_65)) {
                ageSb.append(CommonConstants.SYMBOL_PLUS);
            }
        }
    }

    public static void pageAppend(List<Integer> singlePageTypes, boolean isFirst, StringBuilder pageTypeSb) {
        for (Integer pageType : singlePageTypes) {
            if (!isFirst) {
                pageTypeSb.append(CommonConstants.SYMBOL_COMMA_CHINA);
            }
            pageTypeSb.append(PageType.getPageType(pageType).getShowPageTypeName());

            isFirst = false;
        }
    }


    public static void  userDeviceAppend(List<String> userDevices, StringBuilder userDevicesSb, boolean isFirst) {
        for (String userDevice : userDevices) {
            if (!isFirst) {
                userDevicesSb.append(CommonConstants.SYMBOL_COMMA);
            }
            isFirst = false;
            userDevicesSb.append(userDevice);
        }
    }

    public static void userOSAppend(String osPrefix, String osPostfix, List<String> userOSs, StringBuilder userOSsSb, boolean isFirst) {
        for (String userOS : userOSs) {
            if (!isFirst) {
                userOSsSb.append(CommonConstants.SYMBOL_COMMA);
            }
            isFirst = false;
            userOSsSb.append(osPrefix);
            userOSsSb.append(userOS);
            userOSsSb.append(osPostfix);
        }
    }

    public static  FbUserVo getFbUserVo(FbUser fbUser) {
        FbUserVo fbUserVo = new FbUserVo();
        fbUserVo.setAccessToken(fbUser.getAccessToken());
        fbUserVo.setFbUserId(fbUser.getFbUserId());
        fbUserVo.setName(fbUser.getName());
        fbUserVo.setTimeZone(fbUser.getTimeZone());
        fbUserVo.setUserId(fbUser.getUserId());
        return fbUserVo;
    }

    public static void customAudienceAdd(List<CustomAudience> customAudiences, List<CustomAudience> excludedCustomAudiences,
                                   List<CustomAudience> includedCustomAudiences) {
        for (CustomAudience customAudience : customAudiences) {
            if (null != customAudience.getIsClude() && customAudience.getIsClude()) {
                includedCustomAudiences.add(customAudience);
            } else {
                excludedCustomAudiences.add(customAudience);
            }
            customAudience.setIsClude(null);
        }
    }

    public static void customAudienceAppend(List<CustomAudience> customAudiences, StringBuilder sb, boolean isFirst) {
        for (CustomAudience customAudience : customAudiences) {
            if (!isFirst) {
                sb.append(CommonConstants.SYMBOL_COMMA);
            }
            sb.append(customAudience.getName());
            isFirst = false;
        }
    }

    public static List<String> cloneList(List<String> strList) {
        List<String> temp = new ArrayList<>();
        if(ListUtil.isNotEmpty(strList)) {
            ListUtil.copy(temp,strList);
        }
        return temp;
    }

}
